package com.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Product;
import com.model.User;
import com.service.ShopcartService;
import com.service.UserService;
import com.util.Conversion;
import com.util.JdbcUtil;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet  {
	UserService userService=new UserService();
	ShopcartService shopcartService=new ShopcartService();
	public boolean checkUserIsLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		User user = (User)request.getSession().getAttribute("user");
		if(user==null){
			String mess="暂未登录，请登录后再操作！";
			request.setAttribute("mess",mess);
			request.getRequestDispatcher("userLogin.jsp").forward(request, response);
			return false;
		}	
		return true;
	}
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<User> users = null;
		User user = new User();
		Conversion.convert(user, request);
		users = userService.login(user.getUname(),user.getUpassword());
		Integer cartCount = 0;
		if(!users.isEmpty()){
			user = users.get(0);
			System.out.println(user);
			cartCount=shopcartService.getCartCountByUid(user.getUid());
			request.getSession().setAttribute("cartCount", cartCount);
			request.getSession().setAttribute("user",user);
			response.sendRedirect("index.jsp");
		}else{
			String mess = "登录失败!";
			response.sendRedirect("userLogin.jsp?mess="+mess);
		}
	}
	
	private void checkUname_ajax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("uname");
		List<User> users = userService.queryUserByUname(uname);
		if(!users.isEmpty()){
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		Conversion.convert(user, request);
		try {
			userService.regist(user);
			response.sendRedirect("userLogin.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void toUserCenter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request, response)){
			return;
		}
		response.sendRedirect("userCenter.jsp");
	}
	
	private void toUserUpdateMess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request, response)){
			return;
		}
		response.sendRedirect("userUpdateMess.jsp");
	}
	private void updateMess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		Conversion.convert(user, request);
		System.out.println(user);
		try {
			userService.updateMess(user);
			request.getSession().setAttribute("user",user);
			response.sendRedirect("userCenter.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void toUserUpdatePwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request, response)){
			return;
		}
		response.sendRedirect("userUpdatePwd.jsp");
	}
	private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String uname = request.getParameter("uname");
		String opassword = request.getParameter("opassword");
		String upassword = request.getParameter("upassword");
		
		//验证用户是否存在
		List<User> temp = userService.login(uname, opassword);
		if(temp.isEmpty()){
			request.setAttribute("mess", "原密码有误！！！");
			System.out.println("原密码有误！！！");
			request.getRequestDispatcher("userUpdatePwd.jsp").forward(request, response);
		}else{
			User user= (User)request.getSession().getAttribute("user");
			//更新密码
			user.setUpassword(upassword);
			try {
				userService.updateMess(user);
				request.getSession().setAttribute("user",user);
				response.sendRedirect("userCenter.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
	}
	private void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		response.sendRedirect("index.jsp");
	}
}
